home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / sermgrax / frmmain.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-06-10  |  18.8 KB  |  564 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
  3. Begin VB.Form frmMain 
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Service Manager Example #1"
  6.    ClientHeight    =   3015
  7.    ClientLeft      =   45
  8.    ClientTop       =   615
  9.    ClientWidth     =   7425
  10.    Icon            =   "frmMain.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    ScaleHeight     =   3015
  14.    ScaleWidth      =   7425
  15.    StartUpPosition =   1  'CenterOwner
  16.    Begin VB.Timer Timer 
  17.       Enabled         =   0   'False
  18.       Interval        =   2000
  19.       Left            =   6600
  20.       Top             =   2520
  21.    End
  22.    Begin VB.TextBox txtParameters 
  23.       Height          =   285
  24.       Left            =   120
  25.       TabIndex        =   2
  26.       Top             =   2640
  27.       Width           =   6015
  28.    End
  29.    Begin VB.CommandButton cmdStartup 
  30.       Caption         =   "Sta&rtup..."
  31.       Height          =   375
  32.       Left            =   6240
  33.       TabIndex        =   7
  34.       Top             =   1920
  35.       Width           =   1095
  36.    End
  37.    Begin VB.CommandButton cmdContinue 
  38.       Caption         =   "&Continue"
  39.       Height          =   375
  40.       Left            =   6240
  41.       TabIndex        =   6
  42.       Top             =   1440
  43.       Width           =   1095
  44.    End
  45.    Begin VB.CommandButton cmdPause 
  46.       Caption         =   "&Pause"
  47.       Height          =   375
  48.       Left            =   6240
  49.       TabIndex        =   5
  50.       Top             =   960
  51.       Width           =   1095
  52.    End
  53.    Begin VB.CommandButton cmdStop 
  54.       Caption         =   "S&top"
  55.       Height          =   375
  56.       Left            =   6240
  57.       TabIndex        =   4
  58.       Top             =   480
  59.       Width           =   1095
  60.    End
  61.    Begin VB.CommandButton cmdStart 
  62.       Caption         =   "&Start"
  63.       Height          =   375
  64.       Left            =   6240
  65.       TabIndex        =   3
  66.       Top             =   0
  67.       Width           =   1095
  68.    End
  69.    Begin MSComctlLib.ListView ListView 
  70.       Height          =   2295
  71.       Left            =   120
  72.       TabIndex        =   0
  73.       Top             =   0
  74.       Width           =   6015
  75.       _ExtentX        =   10610
  76.       _ExtentY        =   4048
  77.       View            =   3
  78.       LabelEdit       =   1
  79.       Sorted          =   -1  'True
  80.       LabelWrap       =   -1  'True
  81.       HideSelection   =   0   'False
  82.       FullRowSelect   =   -1  'True
  83.       _Version        =   393217
  84.       ForeColor       =   -2147483640
  85.       BackColor       =   -2147483643
  86.       BorderStyle     =   1
  87.       Appearance      =   1
  88.       NumItems        =   3
  89.       BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  90.          Key             =   "Service"
  91.          Text            =   "Service"
  92.          Object.Width           =   6174
  93.       EndProperty
  94.       BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  95.          SubItemIndex    =   1
  96.          Key             =   "Status"
  97.          Text            =   "Status"
  98.          Object.Width           =   2117
  99.       EndProperty
  100.       BeginProperty ColumnHeader(3) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  101.          SubItemIndex    =   2
  102.          Key             =   "Startup"
  103.          Text            =   "Startup"
  104.          Object.Width           =   2117
  105.       EndProperty
  106.    End
  107.    Begin VB.Label Label1 
  108.       Caption         =   "St&artup Parameters:"
  109.       Height          =   255
  110.       Left            =   120
  111.       TabIndex        =   1
  112.       Top             =   2400
  113.       Width           =   2895
  114.    End
  115.    Begin VB.Menu mnuFile 
  116.       Caption         =   "&File"
  117.       Begin VB.Menu mnuFileSelectComputer 
  118.          Caption         =   "&Select Computer..."
  119.       End
  120.       Begin VB.Menu mnuFileSep1 
  121.          Caption         =   "-"
  122.       End
  123.       Begin VB.Menu mnuFileExit 
  124.          Caption         =   "E&xit"
  125.       End
  126.    End
  127.    Begin VB.Menu mnuService 
  128.       Caption         =   "&Service"
  129.       Begin VB.Menu mnuServiceStart 
  130.          Caption         =   "&Start"
  131.       End
  132.       Begin VB.Menu mnuServiceStop 
  133.          Caption         =   "S&top"
  134.       End
  135.       Begin VB.Menu mnuServicePause 
  136.          Caption         =   "&Pause"
  137.       End
  138.       Begin VB.Menu mnuServiceContinue 
  139.          Caption         =   "&Continue"
  140.       End
  141.       Begin VB.Menu mnuServiceStartup 
  142.          Caption         =   "Sta&rtup..."
  143.       End
  144.       Begin VB.Menu mnuServiceSep1 
  145.          Caption         =   "-"
  146.       End
  147.       Begin VB.Menu mnuServiceCreate 
  148.          Caption         =   "&Create..."
  149.       End
  150.       Begin VB.Menu mnuServiceDelete 
  151.          Caption         =   "&Delete"
  152.       End
  153.       Begin VB.Menu mnuServiceConfigure 
  154.          Caption         =   "Con&figure..."
  155.       End
  156.    End
  157. Attribute VB_Name = "frmMain"
  158. Attribute VB_GlobalNameSpace = False
  159. Attribute VB_Creatable = False
  160. Attribute VB_PredeclaredId = True
  161. Attribute VB_Exposed = False
  162. '****************************************************************************************************
  163. '   Copyright (c) Key Technology Pty Ltd 1999, All Rights Reserved.
  164. '   Web site: http://www.keytech.com.au  Email: info@keytech.com.au
  165. '****************************************************************************************************
  166. Option Explicit
  167. ' The name of the computer whose services are being managed -
  168. ' an empty string means the local computer
  169. Private mComputerName As String
  170. Private Sub FillListView()
  171.     On Error GoTo ErrorHandler
  172.     ' Clear the list view of service information
  173.     ListView.ListItems.Clear
  174.     ' Open the service manager
  175.     Dim ServiceManager As New ServiceManager
  176.     ServiceManager.ComputerName = mComputerName
  177.     ' Check if any services!
  178.     Dim v As Variant
  179.     v = ServiceManager.Services
  180.     If VarType(v) <> (vbString Or vbArray) Then Exit Sub
  181.     ' Display the service names and status
  182.     Dim ServiceName() As String
  183.     Dim i As Integer
  184.     Dim ListItem As ListItem
  185.     Dim Text As String
  186.     ServiceName = v
  187.     For i = LBound(ServiceName) To UBound(ServiceName)
  188.         ' Service display name
  189.         Dim DisplayName As String
  190.         
  191.         DisplayName = ServiceManager.DisplayName(ServiceName(i))
  192.         
  193.         Set ListItem = ListView.ListItems.Add(, , DisplayName)
  194.         
  195.         ' Remember the service name
  196.         ListItem.Tag = ServiceName(i)
  197.         
  198.         ' Status
  199.         Select Case ServiceManager.Status(ServiceName(i))
  200.             Case Running
  201.                 Text = "Started"
  202.                 
  203.             Case StopPending
  204.                 Text = "Stop Pending"
  205.                 
  206.             Case Stopped
  207.                 Text = ""
  208.                 
  209.             Case StartPending
  210.                 Text = "Start Pending"
  211.                 
  212.             Case Paused
  213.                 Text = "Paused"
  214.                 
  215.             Case PausePending
  216.                 Text = "Pause Pending"
  217.                 
  218.             Case ContinuePending
  219.                 Text = "continue Pending"
  220.             
  221.             Case Else
  222.                 Text = "?"
  223.         End Select
  224.         
  225.         ListItem.ListSubItems.Add , "Status", Text
  226.         
  227.         ' Startup
  228.         Select Case ServiceManager.Configuration(ServiceName(i)).StartType
  229.             Case AutoStart
  230.                 Text = "Automatic"
  231.                 
  232.             Case BootStart
  233.                 Text = "Boot"
  234.                 
  235.             Case DemandStart
  236.                 Text = "Manual"
  237.                 
  238.             Case Disabled
  239.                 Text = "Disabled"
  240.                 
  241.             Case SystemStart
  242.                 Text = "System Start"
  243.                 
  244.             Case Else
  245.                 Text = "?"
  246.         End Select
  247.         
  248.         ListItem.ListSubItems.Add , "Startup", Text
  249.     Next
  250.     If ListView.ListItems.Count <> 0 Then
  251.         ListView.ListItems(1).Selected = True
  252.     End If
  253.     Exit Sub
  254. ErrorHandler:
  255.     MsgBox Err.Description, vbExclamation
  256. End Sub
  257. Private Sub RefreshListItem(ServiceManager As ServiceManager, _
  258.                             ListItem As ListItem)
  259.     On Error GoTo ErrorHandler
  260.     Dim ServiceName As String
  261.     Dim Text As String
  262.     ServiceName = ListItem.Tag
  263.     Select Case ServiceManager.Status(ServiceName)
  264.         Case Running
  265.             Text = "Started"
  266.             
  267.         Case StopPending
  268.             Text = "Stop Pending"
  269.             
  270.         Case Stopped
  271.             Text = ""
  272.             
  273.         Case StartPending
  274.             Text = "Start Pending"
  275.             
  276.         Case Paused
  277.             Text = "Paused"
  278.             
  279.         Case PausePending
  280.             Text = "Pause Pending"
  281.             
  282.         Case ContinuePending
  283.             Text = "continue Pending"
  284.         
  285.         Case Else
  286.             Text = "?"
  287.     End Select
  288.     If Text <> ListItem.ListSubItems("Status") Then
  289.         ListItem.ListSubItems("Status") = Text
  290.     End If
  291.     Exit Sub
  292. ErrorHandler:
  293.     MsgBox Err.Description, vbExclamation
  294. End Sub
  295. Private Sub RefreshListView()
  296.     On Error GoTo ErrorHandler
  297.     ' Open the service manager
  298.     Dim ServiceManager As New ServiceManager
  299.     ServiceManager.ComputerName = mComputerName
  300.     ' Update the status for each listed service
  301.     Dim ListItem As ListItem
  302.     Dim ServiceName As String
  303.     Dim Text As String
  304.     For Each ListItem In ListView.ListItems
  305.         RefreshListItem ServiceManager, ListItem
  306.     Next
  307.     Exit Sub
  308. ErrorHandler:
  309.     MsgBox Err.Description, vbExclamation
  310. End Sub
  311. Private Sub SetButtonState()
  312.     On Error GoTo ErrorHandler
  313.     ' Check if service selected
  314.     If ListView.SelectedItem Is Nothing Then
  315.         cmdStart.Enabled = False
  316.         cmdStop.Enabled = False
  317.         cmdPause.Enabled = False
  318.         cmdContinue.Enabled = False
  319.         cmdStartup.Enabled = False
  320.         
  321.         Exit Sub
  322.     End If
  323.     ' Get the service name
  324.     Dim ServiceName As String
  325.     ServiceName = ListView.SelectedItem.Tag
  326.     ' Get the service extended status
  327.     Dim ServiceManager As New ServiceManager
  328.     Dim ExtendedServiceStatus As ExtendedServiceStatus
  329.     ServiceManager.ComputerName = mComputerName
  330.     Set ExtendedServiceStatus = ServiceManager.ExtendedStatus(ServiceName)
  331.     ' Set the menu state
  332.     cmdStart.Enabled = (ExtendedServiceStatus.Status = Stopped)
  333.     cmdStop.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptStop)
  334.     cmdPause.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptPauseContinue)
  335.     cmdContinue.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptPauseContinue)
  336.     Exit Sub
  337. ErrorHandler:
  338.     MsgBox Err.Description, vbExclamation
  339. End Sub
  340. Private Sub Form_Load()
  341.     Timer.Enabled = False
  342.     FillListView
  343.     SetButtonState
  344.     Timer.Enabled = True
  345. End Sub
  346. Private Sub cmdStart_Click()
  347.     On Error GoTo ErrorHandler
  348.     ' Get the service name
  349.     If ListView.SelectedItem Is Nothing Then Exit Sub
  350.     Dim ServiceName As String
  351.     ServiceName = ListView.SelectedItem.Tag
  352.     ' Get the optional startup arguments as a string array
  353.     Dim ArgumentString() As String
  354.     Dim Arguments As Variant
  355.     If Len(txtParameters) <> 0 Then
  356.         ArgumentString = Split(txtParameters, " ")
  357.         Arguments = ArgumentString
  358.     End If
  359.     ' Start the service
  360.     Dim ServiceManager As New ServiceManager
  361.     ServiceManager.ComputerName = mComputerName
  362.     ServiceManager.AccessMode = WriteAccess
  363.     ServiceManager.Start ServiceName, Arguments
  364.     RefreshListItem ServiceManager, ListView.SelectedItem
  365.     Exit Sub
  366. ErrorHandler:
  367.     MsgBox Err.Description, vbExclamation
  368. End Sub
  369. Private Sub cmdStop_Click()
  370.     On Error GoTo ErrorHandler
  371.     ' Get the service name
  372.     If ListView.SelectedItem Is Nothing Then Exit Sub
  373.     Dim ServiceName As String
  374.     ServiceName = ListView.SelectedItem.Tag
  375.     ' Stop the service
  376.     Dim ServiceManager As New ServiceManager
  377.     ServiceManager.ComputerName = mComputerName
  378.     ServiceManager.AccessMode = WriteAccess
  379.     ServiceManager.Stop ServiceName
  380.     RefreshListItem ServiceManager, ListView.SelectedItem
  381.     Exit Sub
  382. ErrorHandler:
  383.     MsgBox Err.Description, vbExclamation
  384. End Sub
  385. Private Sub cmdPause_Click()
  386.     On Error GoTo ErrorHandler
  387.     ' Get the service name
  388.     If ListView.SelectedItem Is Nothing Then Exit Sub
  389.     Dim ServiceName As String
  390.     ServiceName = ListView.SelectedItem.Tag
  391.     ' Pause the service
  392.     Dim ServiceManager As New ServiceManager
  393.     ServiceManager.ComputerName = mComputerName
  394.     ServiceManager.AccessMode = WriteAccess
  395.     ServiceManager.Pause ServiceName
  396.     RefreshListItem ServiceManager, ListView.SelectedItem
  397.     Exit Sub
  398. ErrorHandler:
  399.     MsgBox Err.Description, vbExclamation
  400. End Sub
  401. Private Sub cmdContinue_Click()
  402.     On Error GoTo ErrorHandler
  403.     ' Get the service name
  404.     If ListView.SelectedItem Is Nothing Then Exit Sub
  405.     Dim ServiceName As String
  406.     ServiceName = ListView.SelectedItem.Tag
  407.     ' Continue the service
  408.     Dim ServiceManager As New ServiceManager
  409.     ServiceManager.ComputerName = mComputerName
  410.     ServiceManager.AccessMode = WriteAccess
  411.     ServiceManager.Continue ServiceName
  412.     RefreshListItem ServiceManager, ListView.SelectedItem
  413.     Exit Sub
  414. ErrorHandler:
  415.     MsgBox Err.Description, vbExclamation
  416. End Sub
  417. Private Sub cmdStartup_Click()
  418.     On Error GoTo ErrorHandler
  419.     If ListView.SelectedItem Is Nothing Then Exit Sub
  420.     ' Prompt for startup information
  421.     Dim frm As New frmStartup
  422.     frm.Show vbModal, Me
  423.     If Not frm.ConfigurationChanged Then Exit Sub
  424.     ' Refresh the listview
  425.     Dim ServiceManager As New ServiceManager
  426.     ServiceManager.ComputerName = mComputerName
  427.     RefreshListItem ServiceManager, ListView.SelectedItem
  428.     Exit Sub
  429. ErrorHandler:
  430.     MsgBox Err.Description, vbExclamation
  431. End Sub
  432. Private Sub ListView_Click()
  433.     SetButtonState
  434. End Sub
  435. Private Sub mnuFileExit_Click()
  436.     Unload Me
  437. End Sub
  438. Private Sub mnuFileSelectComputer_Click()
  439.     ' Prompt for the computer name
  440.     Dim frm As New frmSelectComputer
  441.     frm.ComputerName = mComputerName
  442.     frm.Show vbModal, Me
  443.     If frm.ComputerName = mComputerName Then Exit Sub
  444.     mComputerName = frm.ComputerName
  445.     ' Refill the listview
  446.     Timer.Enabled = False
  447.     FillListView
  448.     SetButtonState
  449.     Timer.Enabled = True
  450.     ' Set the caption
  451.     Caption = "Service Manager Example #1"
  452.     If Len(mComputerName) Then Caption = Caption & " - " & mComputerName
  453. End Sub
  454. Private Sub mnuService_Click()
  455.     On Error GoTo ErrorHandler
  456.     ' Check if service selected
  457.     If ListView.SelectedItem Is Nothing Then
  458.         mnuServiceStart.Enabled = False
  459.         mnuServiceStop.Enabled = False
  460.         mnuServicePause.Enabled = False
  461.         mnuServiceContinue.Enabled = False
  462.         mnuServiceStartup.Enabled = False
  463.         mnuServiceDelete.Enabled = False
  464.         mnuServiceConfigure.Enabled = False
  465.         
  466.         Exit Sub
  467.     End If
  468.     ' Get the service name
  469.     Dim ServiceName As String
  470.     ServiceName = ListView.SelectedItem.Tag
  471.     ' Get the service extended status
  472.     Dim ServiceManager As New ServiceManager
  473.     Dim ExtendedServiceStatus As ExtendedServiceStatus
  474.     ServiceManager.ComputerName = mComputerName
  475.     Set ExtendedServiceStatus = ServiceManager.ExtendedStatus(ServiceName)
  476.     ' Set the menu state
  477.     mnuServiceStart.Enabled = (ExtendedServiceStatus.Status = Stopped)
  478.     mnuServiceStop.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptStop)
  479.     mnuServicePause.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptPauseContinue)
  480.     mnuServiceContinue.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptPauseContinue)
  481.     mnuServiceDelete.Enabled = (ExtendedServiceStatus.Status = Stopped)
  482.     mnuServiceConfigure.Enabled = (ExtendedServiceStatus.Status = Stopped)
  483.     Exit Sub
  484. ErrorHandler:
  485.     MsgBox Err.Description, vbExclamation
  486. End Sub
  487. Private Sub mnuServiceConfigure_Click()
  488.     ' Get the service name
  489.     If ListView.SelectedItem Is Nothing Then Exit Sub
  490.     Dim ServiceName As String
  491.     ServiceName = ListView.SelectedItem.Tag
  492.     ' Prompt to configure a service
  493.     Dim frm As New frmConfigure
  494.     frm.ServiceName = ServiceName
  495.     frm.Show vbModal, Me
  496.     If Not frm.ConfigurationChanged Then Exit Sub
  497.     ' Refill the listview
  498.     Timer.Enabled = False
  499.     FillListView
  500.     SetButtonState
  501.     Timer.Enabled = True
  502. End Sub
  503. Private Sub mnuServiceContinue_Click()
  504.     cmdContinue_Click
  505. End Sub
  506. Private Sub mnuServiceCreate_Click()
  507.     ' Prompt to create a service
  508.     Dim frm As New frmConfigure
  509.     frm.Show vbModal, Me
  510.     If Not frm.ConfigurationChanged Then Exit Sub
  511.     ' Refill the listview
  512.     Timer.Enabled = False
  513.     FillListView
  514.     SetButtonState
  515.     Timer.Enabled = True
  516. End Sub
  517. Private Sub mnuServiceDelete_Click()
  518.     On Error GoTo ErrorHandler
  519.     ' Get the service name
  520.     If ListView.SelectedItem Is Nothing Then Exit Sub
  521.     Dim ServiceName As String
  522.     ServiceName = ListView.SelectedItem.Tag
  523.     ' Prompt the user
  524.     If MsgBox("Are you sure you want to delete the service " & ServiceName & "?", _
  525.               vbExclamation Or vbYesNoCancel) <> vbYes Then Exit Sub
  526.               
  527.     ' Delete the service
  528.     Dim ServiceManager As New ServiceManager
  529.     ServiceManager.ComputerName = mComputerName
  530.     ServiceManager.AccessMode = WriteAccess
  531.     ServiceManager.Delete ServiceName
  532.     ' Refill the listview
  533.     Timer.Enabled = False
  534.     FillListView
  535.     SetButtonState
  536.     Timer.Enabled = True
  537.     Exit Sub
  538. ErrorHandler:
  539.     MsgBox Err.Description, vbExclamation
  540. End Sub
  541. Private Sub mnuServicePause_Click()
  542.     cmdPause_Click
  543. End Sub
  544. Private Sub mnuServiceStart_Click()
  545.     cmdStart_Click
  546. End Sub
  547. Private Sub mnuServiceStartup_Click()
  548.     cmdStartup_Click
  549. End Sub
  550. Private Sub mnuServiceStop_Click()
  551.     cmdStop_Click
  552. End Sub
  553. Private Sub Timer_Timer()
  554.     RefreshListView
  555.     SetButtonState
  556. End Sub
  557. Public Property Get ComputerName() As String
  558.     ComputerName = mComputerName
  559. End Property
  560. Public Property Get SelectedServiceName() As String
  561.     If ListView.SelectedItem Is Nothing Then Exit Property
  562.     SelectedServiceName = ListView.SelectedItem.Tag
  563. End Property
  564.